home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / RASPOLY.ZIP / POLY.C next >
Encoding:
C/C++ Source or Header  |  1996-12-03  |  3.0 KB  |  163 lines

  1. //A C polygon drawing demonstration (Borland code)
  2. //if you have a faster routine or can improve this one, please email me -
  3. //compuserve# 101447,2365 (A.McNab)
  4. //note: no clipping
  5. #include "string.h"
  6. #include "dos.h"
  7. #include "mem.h"
  8. #include "conio.h"
  9.  
  10. int polyx[200][2];//stores left & right edge coordinates of polygon
  11. int points[4][2]={10,10, 40,20, 50,40, 5,70};//polygon data
  12. unsigned char far *vgascreen=MK_FP(0xa000,0);//pointer to VGA screen
  13.  
  14. char drawpoly(int np,int *iptr,unsigned char col);
  15. char pline(int x1,int y1,int x2,int y2);
  16. void SetVGA(void);
  17. void SetTXT(void);
  18.  
  19.  
  20. void main(void)
  21. {
  22.   int a,b;
  23.  
  24.   SetVGA();
  25.  
  26.   for(a=0; a<256; a+=6) {
  27.     for(b=0; b<4; b++) points[b][0]+=6;
  28.     drawpoly(4,*points,a);
  29.   }
  30.  
  31.   getch();
  32.  
  33.   SetTXT();
  34. }
  35.  
  36. //draws a polygon to VGA, the points must be in clockwise order
  37. //(excuse the messy pointer stuff)
  38. char drawpoly(int np,int *iptr,unsigned char col)
  39. {
  40.   int top=199,bottom=0;
  41.   int a,b,x1,y1,x2,y2;
  42.   unsigned char far *vga;
  43.  
  44.   //go through polygon lines and calculate x values
  45.   np--;
  46.   for(a=0; a<np; a++) {//do up to last line
  47.  
  48.     b=a*2;
  49.     x1=iptr[b]; y1=iptr[b+1];
  50.     if (y1<top) top=y1;
  51.     if (y1>bottom) bottom=y1;
  52.  
  53.     pline(x1,y1,iptr[b+2],iptr[b+3]);
  54.   }
  55.     //do last line
  56.   x1=iptr[np*2]; y1=iptr[(np*2)+1];
  57.   if (y1<top) top=y1;
  58.   if (y1>bottom) bottom=y1;
  59.   pline(x1,y1,iptr[0],iptr[1]);
  60.  
  61.   if (bottom<top) return(0);//error
  62.  
  63.   vga=vgascreen+(top*320);
  64.  
  65.   //loop from top to bottom of polygon, drawing lines
  66.   for(a=top; a<bottom+1; a++)
  67.   {
  68.     if (polyx[a][0]>polyx[a][1]) {vga=vga+320; continue;}//error
  69.     memset(vga+polyx[a][0],col,polyx[a][1]-polyx[a][0]+1);
  70.     vga=vga+320;
  71.   }
  72.  
  73.   return(1);
  74. }
  75.  
  76. char pline(int x1,int y1,int x2,int y2)
  77. {
  78.   char xu;
  79.   int offset;
  80.   int yd=y2-y1,yd2;
  81.   int xd=x2-x1;
  82.   int err=0;
  83.   int X=x1,Y;
  84.  
  85.   if (y1==y2) return(0);//horizontal line
  86.  
  87.   if (yd<0) yd=-yd;
  88.  
  89.   if (xd<0) {xd=-xd; xu=-1;}
  90.   else xu=1;
  91.  
  92.   //this line makes some edge lines better
  93.   //not essential
  94.   if (yd>xd) err=yd>>1;
  95.  
  96.   //record x values along line
  97.   yd2=yd-1;
  98.  
  99.   if (xu==1) {
  100.  
  101.     if (y1>y2) {
  102.       for (Y=y1; Y>y2-1; Y--)
  103.       {
  104.     polyx[Y][0]=X;
  105.     err+=xd;
  106.     if (err>yd2) do{err-=yd; X++;}while(err>=yd);
  107.       }
  108.       return(0);
  109.     }
  110.     else {
  111.       for (Y=y1; Y<y2+1; Y++)
  112.       {
  113.     polyx[Y][1]=X;
  114.     err+=xd;
  115.     if (err>yd2) do{err-=yd; X++;}while(err>=yd);
  116.       }
  117.       return(0);
  118.     }
  119.  
  120.   }
  121.  
  122.   else {
  123.  
  124.     if (y1>y2) {
  125.       for (Y=y1; Y>y2-1; Y--)
  126.       {
  127.     polyx[Y][0]=X;
  128.     err+=xd;
  129.     if (err>yd2) do{err-=yd; X--;}while(err>=yd);
  130.       }
  131.       return(0);
  132.     }
  133.     else {
  134.       for (Y=y1; Y<y2+1; Y++)
  135.       {
  136.     polyx[Y][1]=X;
  137.     err+=xd;
  138.     if (err>yd2) do{err-=yd; X--;}while(err>=yd);
  139.       }
  140.     }
  141.  
  142.   }
  143.  
  144.   return(0);
  145. }
  146.  
  147. void SetVGA(void)
  148. {
  149.   union REGS regs;
  150.   regs.h.al = 0x13;
  151.   regs.h.ah = 0x00;
  152.   int86(0x10, ®s, ®s);
  153. }
  154.  
  155. void SetTXT(void)
  156. {
  157.   union REGS regs;
  158.   regs.h.al = 0x03;
  159.   regs.h.ah = 0x00;
  160.   int86(0x10, ®s, ®s);
  161. }
  162.  
  163.